<?php

	include_once('getDBConnection.php');
	include_once('FavoriteFood.php');

	$obj = new DBConnection();
	$mysqli = $obj->getDBC();

	if(isset($_POST['date']) && isset($_POST['userId'])){
		$date = $_POST['date'];
		$userId = $_POST['userId'];


		if(empty($userId)){
			return 'Exception userId is empty!';
		}

		$query = "select h.food_id, h.quick_add_id, h.meal_time, h.meal_serving_size, f.name, f.type, f.calories, f.fat, f.fiber, f.points, f.date_created, f.date_modified, f.created_by, q.quick_name, q.quick_points, fav.fav_serving_size from " .
			Constants::FOOD_HISTORY . " h left join " .
			Constants::FOOD_TABLE . " f on h.food_id = f.id left join " .
			Constants::FOOD_FAVORITE . " fav on h.user_id = fav.user_id and h.food_id = fav.food_id left join " .
			Constants::QUICK_ADD . " q on h.quick_add_id = q.quick_add_id where h.user_id = $userId and h.food_date = '$date';";

		$foodResults = $mysqli->query($query);

		if($mysqli->error){
			die("An error occurred while loading information related to the date of $date" . $mysqli->error);
		}

		$count=0;

		$return = "<foods>";//start the return values
		while($row = $foodResults->fetch_assoc()){

			$return .=	"<food><id>".$row[Constants::FOOD_HISTORY_ID]."</id>".
						"<type>".$row[Constants::FOOD_TYPE]."</type>".
						"<calories>".$row[Constants::FOOD_CAL]."</calories>".
						"<fat>".$row[Constants::FOOD_FAT]."</fat>".
						"<fiber>".$row[Constants::FOOD_FIBER]."</fiber>".
						"<servingSize>".$row[Constants::MEAL_SERVING_SIZE]."</servingSize>".//actual amount of food recorded for a particular mealtime
						"<foodFavServingSize>".$row[Constants::FOOD_FAV_SERVING_SIZE]."</foodFavServingSize>".//used for a food that is a favorite food (if 0 then food isn't a favorite)
						"<dateCreated>".$row[Constants::FOOD_DT_CREATED]."</dateCreated>".
						"<dateModified>".$row[Constants::FOOD_DT_MODIFIED]."</dateModified>".
						"<createdBy>".$row[Constants::FOOD_CREATED_BY]."</createdBy>";

			if($row[Constants::FOOD_HISTORY_ID] == 0){//food is a quick add
				$return.= "<name>".$row[Constants::QUICK_FOOD_NAME]."</name>".
						  "<points>".$row[Constants::QUICK_FOOD_POINTS]."</points>";
			}else{
				$return.= "<name>".$row[Constants::FOOD_NAME]."</name>".
						  "<points>".$row[Constants::FOOD_POINTS]."</points>";
			}
			$return .= "<mealTime>".$row[Constants::MEAL_TIME]."</mealTime></food>";
		}

		$return .= "</foods>";
		$foodResults->close();



	}else{//date or userId aren't set so date information cannot be returned
		die('An invalid value was passed to the server preventing date related information from being loaded');
	}

	print($return);
	$mysqli->close();


?>